package com.ebupt.migu.music.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.music.api.service.DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class DataServiceImpl implements DataService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取指定短信策略的下发成功数和失败数
     *
     * @param sid  策略ID
     * @param date 策略执行日期
     * @return
     */
    @Override
    public String success(String sid, String date) {
        String success_key = sid + "_" + date + "_success";
        String fail_key = sid + "_" + date + "_fail";
        String success = "0";
        String fail = "0";
        JSONObject json = new JSONObject();
        if (redisTemplate.hasKey(success_key)) {
            success = String.valueOf(redisTemplate.opsForValue().get(success_key));
        }
        if (redisTemplate.hasKey(fail_key)) {
            fail = String.valueOf(redisTemplate.opsForValue().get(fail_key));
        }
        json.put("success", success);
        json.put("fail", fail);
        log.info("短信下发成功数：{}， 失败数{}", success, fail);
        return json.toJSONString();
    }
}
